package com.wudl.govern.service.impl;

import com.wudl.govern.domain.MappingField;
import com.wudl.govern.domain.MappingSource;
import com.wudl.govern.domain.MappingTable;
import com.wudl.govern.mapper.MappingFieldMapp;
import com.wudl.govern.service.MetaAtlasService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ：wudl
 * @date ：Created in 2022-04-04 22:58
 * @description：
 * @modified By：
 * @version: 1.0
 */

@Service
@Slf4j
public class MetaAtlasServiceImpl implements MetaAtlasService {

    @Autowired
    MappingFieldMapp mappingFieldMapper;

    @Override
    public Integer insertMappingField(List<MappingField> mappingFieldList) {
        try {
            for (MappingField m : mappingFieldList) {
                m.setDataState(1);
                m.setMappingType(2);
            }
            int field = mappingFieldMapper.insertMappingFieldList(mappingFieldList);

            return field;
        } catch (Exception e) {
            log.error("##### 添加数据异常 ##### error={}", e);
            return -1;
        }
    }

    //添加库信息功能
    @Override
    public Integer insertMappingSource(List<MappingSource> mappingSourceList) {
        try {
            MappingSource mappingSource = new MappingSource();
            for (MappingSource m : mappingSourceList) {
                int initSourceIde = m.getTargetSourceId();
                int targetSourceId = m.getInitSourceId();
                String initSourceName = m.getTargetSourceName();
                String targetSourceName = m.getInitSourceName();
                m.setInitSourceId(initSourceIde);
                m.setTargetSourceId(targetSourceId);
                m.setInitSourceName(initSourceName);
                m.setTargetSourceName(targetSourceName);
                m.setDataState(1);
                m.setMappingType(2);
                //通过目标表的id去判断源表在图谱的位置
                MappingSource subordinate = mappingFieldMapper.selectSubordinate(m.getTargetSourceId());
                if (subordinate == null) {
                    //0表示源表不存在
                    m.setPosition(1);
                    mappingSource.setInitSourceId(m.getTargetSourceId());
                    mappingSource.setTargetSourceId(0);
                    mappingSource.setInitCompany(m.getInitCompany());
                    mappingSource.setPosition(0);
                    mappingSource.setDataState(1);
                    mappingSource.setInitSourceName(m.getTargetSourceName());
                    mappingFieldMapper.insertMappingSource(mappingSource);
                } else {
                    //+1是让目标表所处的位置在源表的下方
                    m.setPosition(subordinate.getPosition() + 1);
                }
                //判断该源库是否已经在图谱中已经存在
                MappingTable mid = mappingFieldMapper.selectMappingSourceOnly(m.getInitSourceId(), m.getTargetSourceId());
                if (mid != null) {
                    //如果存在，将历史数据清除掉
                    mappingFieldMapper.updateMappingSourceState(mid.getId());
                }

            }
            int source = mappingFieldMapper.insertMappingSourceList(mappingSourceList);


            return source;
        } catch (Exception e) {
            log.error("##### 添加数据异常 ##### error={}", e);
            return -1;
        }
    }

    //添加表信息功能
    @Override
    public Integer insertMappingTable(List<MappingTable> mappingTableList) {
        try {
            if (mappingTableList != null) {
                for (MappingTable m : mappingTableList) {
                    m.setMappingType(2);
                    m.setDataState(1);
                }
            }
            int table = mappingFieldMapper.insertMappingTableList(mappingTableList);
            return table;
        } catch (Exception e) {
            log.error("##### 添加数据异常 ##### error={}", e);
            return -100;
        }
    }


    //删除库
    @Override
    public void deleteMappingSourceState(Map map) {
        try {
            if (map != null) {
                if (map.get("targetSourceId") != null) {
                    int targetSourtceId = Integer.parseInt(map.get("targetSourceId").toString());
                    //从图谱表中删除库信息
                    int ku = mappingFieldMapper.deleteMappingSourceState(targetSourtceId);
                    if (ku > 0) {
                        //获取被删除的库所属的表
                        List<MappingTable> tableIdList = mappingFieldMapper.selectTbaleIds(targetSourtceId);
                        //从图谱表中删除表信息
                        mappingFieldMapper.deleteMappingTable(targetSourtceId, null);
                        //从图谱表中删除字段信息
                        for (MappingTable m : tableIdList) {
                            mappingFieldMapper.deleteMappingField(m.getTargetTableId(), null);
                        }


                    }
                }
            }
        } catch (Exception e) {
            log.error("##### 数据异常 ##### error={}", e);
        }
    }


    //删除表
    @Override
    public void deleteMappingTable(List<MappingTable> mappingTableLis) {
        try {
            if (mappingTableLis != null) {
                for (MappingTable m : mappingTableLis) {
                    mappingFieldMapper.deleteMappingTable(m.getTargetSourtceId(), m.getTargetTableId());
                    //获取被删除的库所属的表
                    mappingFieldMapper.deleteMappingField(m.getTargetTableId(), null);
//                    }
                }

            }
        } catch (Exception e) {
            log.error("##### 数据异常 ##### error={}", e);
        }
    }

    //删除字段
    @Override
    public void deleteMappingField(List<MappingField> mappingTableLis) {
        try {
            if (mappingTableLis != null) {
                for (MappingField m : mappingTableLis) {
                    mappingFieldMapper.deleteMappingField(m.getTargetTableId(), m.getTargetFieldId());
                }
            }
        } catch (Exception e) {
            log.error("##### 数据异常 ##### error={}", e);
        }
    }
}
